//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values


// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Priority
private alias 1 : PRIORITY_ACTIVE

public value UseFlashlight = 0;
public value MouseEnabled = 0;
public value FlashBattery = 5000;
public value HUDEnabled = 0;
public value CutsceneActive = 0;
//public value Crafting.State = 0;
public value HasChainSaw = 0;
public value TextActive = 0;
public value CharacterTalking = 0;

private alias Object.Value0 : Object.PageCount 
private alias object.value2 : Object.Timer
private alias object.value3 : object.deathtimer
private alias object.value4 : Crafting.State

private alias object[8].value8 : Char.IsTalking



public alias saveRAM[295]: saved.mobile
public alias saveRAM[296]: saved.transmobile


reserve function MainHUD
reserve function TouchControls
reserve function CraftingUI
reserve function PlayerHPStandalone

function PlayerHPStandalone
		temp1 = 65
		temp2 = screen.ycenter
		temp2 -= 0x50

		if stage.playerlistpos <= 1
			DrawSpriteScreenXY(35, temp1, temp2)

			if PlayerHealth >= 5
				DrawSpriteScreenXY(34, temp1, temp2)
			end if

			temp1 += 0x18

			DrawSpriteScreenXY(35, temp1, temp2)

			if PlayerHealth >= 4
				DrawSpriteScreenXY(34, temp1, temp2)
			end if

			temp1 += 0x18
		end if

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 3
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 2
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 1
			DrawSpriteScreenXY(34, temp1, temp2)
		end if
end function

function CraftingUI
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 180)

		//THE BOOKS
		switch Crafting.State
		case 0
			//if it isnt already
			PlayerHealth = 3
			crafting.state++
		break
		case 1
			object.timer++
			if object.timer == 270
				CheckEqual(screen.xsize, 426)
				temp0 = checkResult
				//if temp0 == 1
				//	PlayMusic(2)
					object.timer = 0
					crafting.state++
				// else
				// 	HasChainSaw = 1
				// 	StopMusic()
				// 	object.timer = 0
				// 	PlaySfx(SfxName[sans], 0)
				// 	crafting.state = 7
				// end if
			end if
		break
		case 2	//first open
			temp4 = screen.xcenter
			DrawSpriteScreenXY(36, temp4, screen.ycenter)//the board


			
			temp0 = screen.xcenter
			temp0 -= 100
			temp3 = screen.xcenter
			temp3 -= 50
			CheckTouchRect(temp0, 64, temp3, 96)
			if checkResult > -1
				temp1 = screen.xcenter
				temp1 -= 77
				temp2 = screen.ycenter
				temp2 -= 48
				DrawSpriteScreenXY(37, temp1, temp2)
				Crafting.State++
			PlaySfx(SfxName[Score Add], 0)				
			end if

		break
		case 3	//opened book
			temp3 = screen.xcenter	
			temp3 += 72
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board
			DrawSpriteScreenXY(39, temp3, screen.ycenter)//the reciepe book open


			temp0 = screen.xcenter
			temp0 -= 170
			temp2 = screen.xcenter
			temp2 -= 120
			CheckTouchRect(temp0, 64, temp2, 96)
			if checkResult > -1
				PlaySfx(SfxName[Score Add], 0)	
				Crafting.State++			
			end if
		break
		case 4
			temp3 = screen.xcenter
			temp3 += 72
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board
			DrawSpriteScreenXY(39, temp3, screen.ycenter)//the reciepe book open

			DrawSpriteScreenXY(42, temp3, screen.ycenter)//recipe

			temp0 = 0
			temp0 += screen.xsize
			temp0 -= 120
			temp2 = 0
			temp2 += screen.xsize
			CheckTouchRect(temp0, 64, temp2, 96)
			if checkResult > -1
				PlaySfx(SfxName[Lamp Post], 0)
				Crafting.State++			
			end if
			
		break
		case 5
			temp3 = screen.xcenter
			temp3 += 72
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board
			DrawSpriteScreenXY(39, temp3, screen.ycenter)//the reciepe book open

			DrawSpriteScreenXY(44, temp3, screen.ycenter)//recipe

			DrawSpriteScreenXY(43, touchscreen[0].xpos, touchscreen[0].ypos)
		
			temp0 = screen.xcenter
			temp0 -= 20
			temp2 = screen.xcenter
			temp2 += 30
			CheckTouchRect(temp0, 170, temp2, 202)
			if checkResult > -1
				PlaySfx(SfxName[Score Add], 0)	
				object[2].value15 = 1	
				crafting.state++
			end if		



		
		
		break
		case 6
			temp3 = screen.xcenter
			temp3 += 72
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board
			DrawSpriteScreenXY(39, temp3, screen.ycenter)//the reciepe book open

			DrawSpriteScreenXY(44, temp3, screen.ycenter)//recipe

			temp3 -= 74
			temp4 = screen.ycenter
			temp4 += 60
			DrawSpriteScreenXY(45, temp3, temp4)//recipe



			object.timer++
			if object.timer == 100
				object.timer = 0
				StopMusic()
				PlaySfx(SfxName[sans], 0)
				object[2].value15 = 1
				crafting.state++
			end if

		break
		case 7
			DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
			object.timer++
			if object.timer == 50
				object.timer = 0
				object[2].value15 = 1 //amkng sure
				PlaySfx(SfxName[sans], 0)
				MouseEnabled = 0 
				music.volume = 100
				PlayMusic(3)
				crafting.state++
			end if			
		break
		case 8

		break
		end switch
	
		//NUMBERS
		
		if crafting.state > 1
			if crafting.state < 7
				temp1 = screen.xcenter
				if Crafting.State >= 3
					temp1 += 16
				else
					temp1 -= 56
				end if
				temp2 = screen.ycenter
				temp2 += 69
				if Crafting.State < 4	//12	
					DrawSpriteScreenXY(40, temp1, temp2)
				else
					DrawSpriteScreenXY(41, temp1, temp2)
				end if
			end if	
		end if	


end function

function MainHUD
		//FLASHLIGHT
		UseFlashlight = 1
		if ThunderActive == 0
			if UseFlashlight == 1
				if FlashBattery > 0	//battery greater then 0?
					if object[2].controlmode != -1	//can the player move?
						if inputdown[2].buttonY == 0	//if your holding the button
							DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 224)
						else
						
							if inputpress[2].buttonY == 1
								PlaySfx(SfxName[Flashlight], 0)
							end if

							FlashBattery -= 1

							temp0=128
							temp0*=Object.Scale
							temp0>>=9
							

							temp5=256
							temp5*=Object.Scale
							temp5>>=9

							temp1=screen.xcenter
							temp1-=temp0

							temp2=temp1
							temp2+=temp5

							temp3=120
							temp3-=temp0

							temp4=temp3
							temp4+=temp5

							DrawRect(0, 0, temp1, screen.ysize, 0x00, 0x00, 0x00, 205)
							DrawRect(temp2, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 205)
							DrawRect(temp1, 0, temp5, temp3, 0x00, 0x00, 0x00, 205)
							DrawRect(temp1, temp4, temp5, screen.ysize, 0x00, 0x00, 0x00, 205)
							
							temp6 = screen.xsize
							temp6 -= screen.xcenter
							object.InkEffect = INK_ALPHA
							object.alpha = 200							
							DrawSpriteScreenFX(15,FX_INK,temp6,screen.ycenter)
							object.InkEffect = INK_ALPHA
							object.alpha = 200
						end if		
					else
						DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 205)	
					end if	
				else	//out of power ur cooked
					DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 224)

					if inputpress[2].buttonY == 1
						PlaySfx(SfxName[Error], 0)
					end if
				end if		
			end if		
		else
			DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 160)	
		end if	


		//BATTERY
	
		temp3 = 210
	
		DrawSpriteScreenXY(17, temp3, screen.ycenter)

		//
		if FlashBattery > 4000
			DrawSpriteScreenXY(18, temp3, screen.ycenter)
		end if

		if FlashBattery > 3000
			DrawSpriteScreenXY(19, temp3, screen.ycenter)
		end if	

		if FlashBattery > 2000
			DrawSpriteScreenXY(20, temp3, screen.ycenter)
		end if		

		if FlashBattery > 1000
			DrawSpriteScreenXY(21, temp3, screen.ycenter)
		end if	

		if FlashBattery > 0
			DrawSpriteScreenXY(22, temp3, screen.ycenter)
		end if	

		//HEALTH

		temp1 = 65
		temp2 = screen.ycenter
		temp2 -= 0x50

		if stage.playerlistpos <= 1
			DrawSpriteScreenXY(35, temp1, temp2)

			if PlayerHealth >= 5
				DrawSpriteScreenXY(34, temp1, temp2)
			end if

			temp1 += 0x18

			DrawSpriteScreenXY(35, temp1, temp2)

			if PlayerHealth >= 4
				DrawSpriteScreenXY(34, temp1, temp2)
			end if

			temp1 += 0x18
		end if

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 3
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 2
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 1
			DrawSpriteScreenXY(34, temp1, temp2)
		end if



		// Find the initial X Position to draw the HUD elements
		if stage.playerlistpos <= 1
			temp1 = 135
		else 
			temp1 = 95
		end if
		
		// Draw the "UFO" graphic
		DrawSpriteScreenXY(10, temp1, 20)
		
		// Draw the first digit of the UFOs counter
		temp0 = Object.PageCount
		temp0 /= 10
		temp1 += 24
		DrawSpriteScreenXY(temp0, temp1, 21)
		
		// Draw the second digit of the UFOs counter
		temp0 = Object.PageCount
		temp0 %= 10
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 21)

end function

function TouchControls
	object.inkEffect = INK_ALPHA
	if CutsceneActive == 0
		temp0 = screen.xsize
		temp0 -= 44
		DrawSpriteScreenXY(49,  26,1 49) //dpad
		temp0 -= 39

		DrawSpriteScreenXY(56, 0, temp0, 8) //pause
		
		
		//movement
		
		temp0 -= 10
		if inputDown.Left==1
			DrawSpriteScreenXY(52,  26, 151)
		endif
		if inputDown.Down==1
			DrawSpriteScreenXY(51,  26, 189)
		end if
		if inputDown.Right==1
			DrawSpriteScreenXY(53,  64, 151)
		endif
		if inputDown.Up==1
			DrawSpriteScreenXY(50,  26, 151)
		endif
		
		temp0 += 15	
		//jump
		if inputDown.buttonA==1
			DrawSpriteScreenXY(55,  temp0, 163)	
		else
			DrawSpriteScreenXY(54,  temp0, 163)
		endif

		if UseFlashlight == 1
			temp0 -= 50
			if crafting.state < 8
				//flashlight
				if inputDown.buttonY==1
					DrawSpriteScreenXY(58,  temp0, 54)	
				else
					DrawSpriteScreenXY(57,  temp0, 54)
				endif	
			end if
		end if

		temp0 += 80
	else
		temp0 = screen.xsize
		temp0 -= 40
		if Char.IsTalking == 1
			DrawSpriteScreenXY(60,  temp0, 12)
		else
			
		end if
	end if
end function



event ObjectDraw
//Not Dead
if HUDEnabled == 1
	if object[2].state != 13
		CallFunction(MainHUD)
	else	
		//Dead
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 224)
	end if
else
	if CutsceneActive == 1
		CallFunction(CraftingUI)
	else
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 180)
		//not dead	
		if object[2].state != 13
			CallFunction(PlayerHPStandalone)
		end if
	end if
end if

	//jumpscare


	//touch controls
	if saved.mobile == 1
		CallFunction(TouchControls)
		DrawSpriteScreenXY(16, touchscreen[0].xpos, touchscreen[0].ypos)
	end if


	if MouseEnabled == 1
		DrawSpriteScreenXY(16, touchscreen[0].xpos, touchscreen[0].ypos)
	end if

	if object[6].state > 3
		if object[2].state == 13
			object.deathtimer++
			if object.deathtimer == 1
				PlayMusic(5)
			end if	
		end if
	end if

	if object[8].state == 8
		if object[2].state != 13
			if BossActive ==  1
				temp1 = 16
				temp2 = screen.ycenter
				temp2 -= 0x45

				DrawSpriteScreenXY(61, temp1, temp2)

				temp1 += 32
				temp2 += 8
				DrawNumbers(0, temp1, temp2, object[6].Value11, 3, 8, 0)
			end if
		end if
	end if
	//debugging


	// if stage.debugmode == 1

	// 	//slenderman state
	// 	DrawNumbers(0, 8, 25, object[6].state, 3, 8, 0)
	// 	//slenderman timer
	// 	DrawNumbers(0, 16, 48, object[2].value16, 3, 8, 0)

	// 	DrawNumbers(0, 16, 72, PlayerHealth, 3, 8, 0)

	// 	DrawNumbers(0, 16, 96, object[6].value7, 3, 8, 0)

	// end if

end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[4].Type = TypeName[HUD JR]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[4].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[4].DrawOrder = 5
	
	HUDEnabled = 1



	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 206, 84)
	SpriteFrame(0, 0, 8, 13, 215, 84)
	SpriteFrame(0, 0, 8, 13, 224, 84)
	SpriteFrame(0, 0, 8, 13, 233, 84)
	SpriteFrame(0, 0, 8, 13, 242, 84)
	SpriteFrame(0, 0, 8, 13, 206, 98)
	SpriteFrame(0, 0, 8, 13, 215, 98)
	SpriteFrame(0, 0, 8, 13, 224, 98)
	SpriteFrame(0, 0, 8, 13, 233, 98)
	SpriteFrame(0, 0, 8, 13, 242, 98)
	
	// 10 - UFO Counter Graphic
	SpriteFrame(0, 0, 24, 16, 206, 58)
	
	// 11 - Time Graphic
	SpriteFrame(0, 0, 24, 8, 231, 58)
	
	// 12 - Rings Graphic
	SpriteFrame(0, 0, 31, 8, 206, 75)
	
	// 13 - Single Tick Graphic
	SpriteFrame(0, 0, 8, 13, 224, 112)
	
	// 14 - Double Tick Graphic
	SpriteFrame(0, 0, 8, 13, 233, 112)


	//15
	SpriteFrame(-128,-128,256,256,256,256)	


	// 16 - Double Tick Graphic
	SpriteFrame(0, 0, 12, 21, 245, 1)


	// 17 - battery bar
	SpriteFrame(-200, -32, 64, 64, 257, 116)


	// 18 - battery bar
	SpriteFrame(-194, -22, 24, 8, 322, 116)
	SpriteFrame(-194, -12, 24, 8, 322, 126)//19
	SpriteFrame(-194, -2, 24, 8, 322, 136)	//20
	SpriteFrame(-194,  8, 24, 8, 322, 146)	//21
	SpriteFrame(-194,  18, 24, 8, 322, 156)	//22



	// 23 - SLENDERMAN!!
	SpriteFrame(-30, -24, 63, 112, 339, 1)


	SpriteFrame(-34, -30, 68, 30, 0, 412) //107 Static
	SpriteFrame(-34, -30, 68, 30, 0, 442)//25
	SpriteFrame(-34, -30, 68, 30, 0, 472)//26
	SpriteFrame(-34, -30, 68, 30, 68, 412)//27
	SpriteFrame(-34, -30, 68, 30, 68, 442)//28
	SpriteFrame(-34, -30, 68, 30, 68, 472)//29
	SpriteFrame(-34, -30, 68, 30, 136, 412)//30
	SpriteFrame(-34, -30, 68, 30, 136, 442)//31
	SpriteFrame(-34, -30, 68, 30, 136, 472)//32


	SpriteFrame(-24, -30, 64, 64, 0, 413) //33


	// lazy filler
	SpriteFrame(-48, -24, 20, 24, 52, 59) //34 ring
	SpriteFrame(-48, -23, 20, 22, 83, 180) //35 ring
	SpriteFrame(-82, -82, 176, 166, 1, 513)	//36	Crafting UI
	SpriteFrame(0, 0, 20, 18, 178, 580)	//37	buton
	SpriteFrame(0, 0, 0, 0, 0, 0)	//38
	SpriteFrame(-240, -82, 147, 166, 199, 513)	//39	Reciepe UI
	SpriteFrame(0, 0, 18, 7, 178, 615)	//40 12x
	SpriteFrame(0, 0, 18, 7, 178, 623)	//41 7x
	SpriteFrame(-53, -66, 116, 54, 347, 513)	//42 the thing sob
	SpriteFrame(0, 0, 18, 18, 464, 513)	//43 paper chainsaw
	SpriteFrame(-53, -66, 116, 54, 347, 568)	//44 the thing sob
	SpriteFrame(0, 0, 16, 16, 8, 680)	//45 chainsaw slotted
	SpriteFrame(0, 0, 12, 21, 245, 1)	//46
	SpriteFrame(0, 0, 12, 21, 245, 1)	//47
	SpriteFrame(0, 0, 12, 21, 245, 1)	//48	

	SpriteFrame(0,0,64,66,126,257) //49 DPAD
	SpriteFrame(26,0,12,25,242,321) //50 DPAD up press
	SpriteFrame(26,0,12,26,242,359) //51 DPAD down press
	SpriteFrame(0,25,26,13,215,358) //52 DPAD left press
	SpriteFrame(0,25,26,13,215,372) //53 DPAD right press
	SpriteFrame(0,0,48,48,191,257) //54 A button
	SpriteFrame(0,0,48,48,191,306) //55 A button down

	SpriteFrame(32,0,16,16,174,325) // 56 Pause Button

	SpriteFrame(0,0,33,33,123,325) // 57 Super Button - Mobile

	SpriteFrame(0,0,33,33,89,325) // 58 Super Button - Mobile

	SpriteFrame(0,0,16,16,174,325) // 59 Pause Button again

	SpriteFrame(0,0,34,34,165,360) // 60 Pause Button

	SpriteFrame(0,0,22,32,513,234) // 61 Slenderhead
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
